package com.chain.BrotherExchange;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.util.Log;

import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.utils.AppLifecycleCallbacks;
import com.meiqia.core.MQManager;
import com.meiqia.core.callback.OnInitCallback;
import com.meiqia.meiqiasdk.util.MQConfig;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.commonsdk.UMConfigure;

import java.util.Map;

public class App extends Application implements AppLifecycleCallbacks.AppDestroyedCallback {

    static {
        SmartRefreshLayout.setDefaultRefreshHeaderCreater((context, layout) ->
                new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate));

        SmartRefreshLayout.setDefaultRefreshFooterCreater((context, layout) ->
                new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate));
    }

    public IWXAPI mWxApi;

    private static App INSTANCE;

    public static App getInstance() {
        return INSTANCE;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        INSTANCE = this;

        // 观察应用生命周期
        registerActivityLifecycleCallbacks(new AppLifecycleCallbacks(this));

        // 文件选择兼容
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builders = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builders.build());
        }

        // 微信注册
        mWxApi = WXAPIFactory.createWXAPI(this, BuildConfig.WE_CHAT_APP_ID, false);
        mWxApi.registerApp(BuildConfig.WE_CHAT_APP_ID);

        // 友盟统计
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, null);

        // 客服注册
        initCustomerServices();

        // 打印sp数据
        printSharedPreferences();
    }

    private void printSharedPreferences() {
        if (BuildConfig.DEBUG) {
            Map<String, Object> map = (Map<String, Object>) getSharedPreferences(
                    Androids.LocalConfig.NAME, Context.MODE_PRIVATE).getAll();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                Log.d("UNT", entry.getKey() + "-" + entry.getValue());
            }
        }
    }

    private void initCustomerServices() {
        MQManager.setDebugMode(BuildConfig.DEBUG);
        MQConfig.init(this, BuildConfig.SERVICE_APP_KEY, new OnInitCallback() {
            @Override
            public void onSuccess(String clientId) {
                if (BuildConfig.DEBUG) Log.d("App", "meiQia:" + clientId);
            }

            @Override
            public void onFailure(int code, String message) {
                if (BuildConfig.DEBUG) Log.d("App", "meiQia:" + message);
            }
        });
    }

    @Override
    public void onAppDestroyed() {
        MQManager.getInstance(this).closeMeiqiaService();
    }
}
